package com.cg.asm.body.service.org.impl;

import com.cg.asm.body.dao.org.OrgMapper;
import com.cg.asm.body.domain.dto.org.OrgRegDTO;
import com.cg.asm.body.domain.vo.org.OrgExpandedByCodeVO;
import com.cg.asm.body.domain.vo.org.OrgManagerTreeVO;
import com.cg.asm.body.domain.vo.org.OrgUpdateStatus;
import com.cg.asm.body.service.org.IOrgService;
import com.cg.asm.body.util.AsmBodyCommonUtil;
import com.cg.asm.client.AsmUUID;
import com.cg.asm.client.exception.AsmException;
import com.github.pagehelper.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class OrgService implements IOrgService {

    @Resource
    private OrgMapper orgMapper;

    @Override
    public List<OrgManagerTreeVO> getOrgTreeDataFullCol() {
        return orgMapper.getOrgTreeDataFullCol();
    }

    @Override
    public OrgExpandedByCodeVO expandedCodes(String orgCode) {
        return orgMapper.expandedCodes(orgCode);
    }

    @Override
    public void saveOrg(OrgRegDTO orgRegDTO) {
        if (null != orgRegDTO && StringUtil.isNotEmpty(orgRegDTO.getOrgName())) {
            orgRegDTO.setOrgCode(AsmUUID.generate());
            orgRegDTO.setCreatorUserId(AsmBodyCommonUtil.getCurrentUserLoginVO().getUserId());
            orgRegDTO.setCreatorDate(new Date());
            orgRegDTO.setSelfLevelCode(AsmUUID.generate());
            orgMapper.saveOrg(orgRegDTO);
        } else {
            throw new AsmException("保存功能清单失败，未获取到需增加的信息，或组织机构名称为空！");
        }
    }

    @Override
    public void delOrg(String orgCode) {
        if (StringUtil.isNotEmpty(orgCode)) {
            orgMapper.delOrg(orgCode);
        } else {
            throw new AsmException("删除失败，组织机构编码不能为空!");
        }
    }

    @Override
    public void saveEditOrg(OrgRegDTO orgRegDTO) {
        orgMapper.saveEditOrg(orgRegDTO);
    }

    @Override
    public void updateStatusBatch(OrgUpdateStatus orgUpdateStatus) {
        orgMapper.updateStatusBatch(orgUpdateStatus);
    }
}
